//
//  SubCollectionViewCellTwo.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 lanou.com. All rights reserved.
//
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT self.bounds.size.height

#import "SubCollectionViewCellTwo.h"
#import "ModelForCollectionViewTwo.h"
#import "UIImageView+WebCache.h"


#import "NetworkHandlerBlock.h"
#import "ModelForTypeOfDetail.h"
#import "ModelForTypeOfListOfDetail.h"

#import "DetailOfBookViewController.h"
#import "ModelForListen.h"

@interface SubCollectionViewCellTwo ()

@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) NSMutableArray *arrTempCoverPath;
@property (nonatomic, retain) NSMutableArray *arrTempTitle;


@property (nonatomic, retain) NSMutableArray *arrFor3DTiYanGuan;
@property (nonatomic, retain) NSMutableArray *arrForTuoKouXiu;
@property (nonatomic, retain) NSMutableArray *arrForZiXun;
@property (nonatomic, retain) NSMutableArray *arrForQingGanShengHuo;
@property (nonatomic, retain) NSMutableArray *arrForLiShiRenWen;
@property (nonatomic, retain) NSMutableArray *arrForWaiYu;

@end

@implementation SubCollectionViewCellTwo

- (void)dealloc {

    [_view release];
    [_arrTempCoverPath release];
    [_arrTempTitle release];
    [super dealloc];

}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.arrFor3DTiYanGuan = [NSMutableArray array];
        self.arrForTuoKouXiu = [NSMutableArray array];
        self.arrForZiXun = [NSMutableArray array];
        self.arrForQingGanShengHuo = [NSMutableArray array];
        self.arrForLiShiRenWen = [NSMutableArray array];
        self.arrForWaiYu = [NSMutableArray array];
        
        [self createSubViews];
        [self handleData];
    }
    return self;
}



- (void)handleData {
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=26&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrFor3DTiYanGuan addObject:modelOfBook];
            }
        }
    }];
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=25&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForTuoKouXiu addObject:modelOfBook];
            }
        }
    }];
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=1&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForZiXun addObject:modelOfBook];
            }
        }
    }];
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=10&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    /////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForQingGanShengHuo addObject:modelOfBook];
            }
        }
    }];
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=9&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ////////////
                   ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForLiShiRenWen addObject:modelOfBook];
            }
        }
    }];
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=5&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForWaiYu addObject:modelOfBook];
            }
        }
    }];
   
    

}
- (void)createSubViews {
    
    self.view = [[UIView alloc]initWithFrame:self.bounds];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.view];
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            UIImageView *picture = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
            picture.frame = CGRectMake(WIDTH / 2 * j + 10,HEIGHT / 3  * i + 10,HEIGHT / 3 - 20,HEIGHT / 3 - 20);
            [self.view addSubview:picture];
            picture.tag = 10*i + j + 1;
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(WIDTH / 2 * j, HEIGHT / 3 * i, WIDTH / 2 - 2, HEIGHT / 3 - 2);
            [button setTitleEdgeInsets:UIEdgeInsetsMake(10, HEIGHT / 3 , 10, 10)];
            [button setTitle:@"3D体验馆" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [self.view addSubview:button];
            button.tag = 100*i + j + 15;
        }
    }
}

- (void)passModelForCellTwo:(NSArray *)arrForModel {
    if (arrForModel) {
        
    
    self.arrTempCoverPath = [NSMutableArray array];
    self.arrTempTitle = [NSMutableArray array];
    for (ModelForCollectionViewTwo *model in arrForModel) {
        [self.arrTempCoverPath addObject:model.coverPath];
        [self.arrTempTitle addObject:model.title];
    }
    
    // 此处赋值时注意button和picture的tag标记的重复.
    UIImageView *pic = [self.view viewWithTag:1];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[5]]];
    
    pic = [self.view viewWithTag:2];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[6]]];
    
    
    pic = [self.view viewWithTag:11];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[7]]];
    
    pic = [self.view viewWithTag:12];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[8]]];
    
    pic = [self.view viewWithTag:21];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[9]]];
    
    pic = [self.view viewWithTag:22];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[10]]];
    
    UIButton *button = [self.view viewWithTag:15];
    [button setTitle:self.arrTempTitle[5] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(enterToDetailOfBookViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    
    button = [self.view viewWithTag:16];
    [button setTitle:self.arrTempTitle[6] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(enterToDetailOfBookViewController1:) forControlEvents:UIControlEventTouchUpInside];
    
    
    button = [self.view viewWithTag:115];
    [button setTitle:self.arrTempTitle[7] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(enterToDetailOfBookViewController2:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:116];
    [button setTitle:self.arrTempTitle[8] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
     [button addTarget:self action:@selector(enterToDetailOfBookViewController3:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:215];
    [button setTitle:self.arrTempTitle[9] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
     [button addTarget:self action:@selector(enterToDetailOfBookViewController4:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:216];
    [button setTitle:self.arrTempTitle[10] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
     [button addTarget:self action:@selector(enterToDetailOfBookViewController5:) forControlEvents:UIControlEventTouchUpInside];
    
    
    }
}


- (void)enterToDetailOfBookViewController :(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrFor3DTiYanGuan;
    bookVC.title = @"3D体验馆";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController1:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForTuoKouXiu;
    bookVC.title = @"脱口秀";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController2:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForZiXun;
    bookVC.title = @"资讯";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController3:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForQingGanShengHuo;
    bookVC.title = @"情感生活";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController4:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForLiShiRenWen;
    bookVC.title = @"历史人文";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController5:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForWaiYu;
    bookVC.title = @"外语";
    [vc.navigationController pushViewController:bookVC animated:NO];
}

@end
